package com.dk.controller;

import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.entity.TCar;
import com.dk.entity.TValuationDTO;
import com.dk.feign.ValuationFeign;
import com.dk.service.ITCarService;
import io.swagger.annotations.*;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping({"carController"})
@Api("卖车服务")
public class CarController {

    @Autowired
    private ValuationFeign valuationFeign;

    @Autowired
    private ITCarService itCarService;

    @PostMapping
    @ApiOperation("录入车辆信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tValuation",value = "估值实体类",required = true,paramType = "body")
    })
    public Result sellCar(TCar tCar,@RequestBody TValuationDTO tValuation){
        //调用估值服务获取估值
        Result<BigDecimal> result = valuationFeign.getvaluationPrice(tValuation.getMileage(),tValuation.getDamage(),tValuation.getPrice(),tValuation.getStatedate().toString());
        BigDecimal vprice = (BigDecimal)result.getResult();
        tCar.setPrice(vprice);
        //将车辆信息保存在数据库
        Boolean b = itCarService.insertCar(tCar);
        return b ? Result.bulid(ConstantEnum.SUCCESS).setResult("车辆信息增加成功") : Result.bulid(ConstantEnum.ERROR).setResult("车辆信息增加失败");
    }

    //查询所有二手车信息
    @GetMapping
    @ApiOperation("查询所有二手车信息")
    public Result getCarList(){
        List<TCar> carList = itCarService.selectCar();
        return Result.bulid(ConstantEnum.SUCCESS).setResult(carList);
    }

    //修改车辆信息
    @PutMapping
    @ApiOperation("修改车辆信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tCar",value = "要修改的车辆信息实体类id",required = true,paramType = "body")
    })
    public Result updateCar(@RequestBody TCar tCar){
        boolean b = itCarService.updateById(tCar);
        if (b){
            return Result.bulid(ConstantEnum.UPDATE_SUCCESS);
        }
        return Result.bulid(ConstantEnum.ERROR);
    }
}
